<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.isCs" clearable placeholder="是否超时">
          <el-option label='未超时' value="0">未超时</el-option>
          <el-option label='已超时' value="1">已超时</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        show-overflow-tooltip
        label="标题">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        show-overflow-tooltip
        label="内容">
      </el-table-column>
      <el-table-column
        prop="fileName"
        header-align="center"
        align="center"
        label="文件">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.fileName" style="cursor: pointer" @click="handleDownload(scope.row.fileId)" size="small">{{scope.row.fileName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="receiveUnitName"
        header-align="center"
        align="center"
        label="接收单位名称">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" size="small">待接收</el-tag>
          <el-tag v-else-if="scope.row.status == 1" size="small">待处理</el-tag>
          <el-tag v-else-if="scope.row.status == 2" size="small">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="beginTime"
        header-align="center"
        align="center"
        label="任务开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="任务结束时间">
      </el-table-column>
      <el-table-column
        prop="handleName"
        header-align="center"
        align="center"
        label="办理人名称">
      </el-table-column>
      <el-table-column
        prop="handleTime"
        header-align="center"
        align="center"
        label="办理时间">
      </el-table-column>
      <el-table-column
        prop="isCs"
        header-align="center"
        align="center"
        label="是否超时">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isCs == 0 || nowDate <= scope.row.endTime" size="small" >未超时</el-tag>
          <el-tag v-else-if="scope.row.isCs == 1 || (nowDate >= scope.row.endTime && scope.row.isCs ==null)" size="small">已超时</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-circle-check" v-if="scope.row.status == 0" round size="small" type="primary" @click="acceptHandle(scope.row.id)">接收
          </el-button>
          <el-button icon="el-icon-circle-check" v-if="scope.row.status == 1" round size="small" type="primary" @click="banliHandle(scope.row.id,scope.row.beginTime)">处理
          </el-button>
          <el-button type="text" size="small" @click="infoHandle(scope.row.id)"><i class="el-icon-info"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <info v-if="infoVisible" ref="info" @refreshDataList="getDataList"></info>
  </div>
</template>

<script>
  import AddOrUpdate from "./task-add-or-update";
  import Info from './task-info'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        nowDate:'',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        infoVisible:false
      }
    },
    components: {
      AddOrUpdate,
      Info
    },
    activated () {
      this.getCurrentTime();
      this.getDataList()
    },
    methods: {
      getCurrentTime() {
        var date = new Date();//当前时间
        var year = date.getFullYear(); //返回指定日期的年份
        var month = this.repair(date.getMonth() + 1);//月
        var day = this.repair(date.getDate());//日
        var hour = this.repair(date.getHours());//时
        var minute = this.repair(date.getMinutes());//分
        var second = this.repair(date.getSeconds());//秒

        //当前时间
        var curTime = year + "-" + month + "-" + day
          + " " + hour + ":" + minute + ":" + second;
        this.nowDate = curTime;
      },
      repair(i) {
        if (i >= 0 && i <= 9) {
          return "0" + i;
        } else {
          return i;
        }
      },
      infoHandle(id){
        this.infoVisible = true;
        this.$nextTick(() => {
          this.$refs.info.init(id)
        })
      },
      handleDownload(id) {
        window.open(this.$http.adornUrl('/api-file/file/download/file?fileId=' + id))
      },
      // 获取数据列表
      getDataList (pageIndex) {
        if (pageIndex) {
          this.pageIndex = pageIndex
        }
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl('/api-operation/task/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'isCs':this.dataForm.isCs
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list;
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = [];
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val;
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      acceptHandle(id){
        this.$http({
          url: this.$http.adornUrl(`/api-operation/task/update`),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
            'status': 1
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '已接收该任务',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      banliHandle(id,beginTime){
        var date = new Date();
        var date1=new Date(beginTime);
        if(date<date1){
          this.$message.error("未到达任务时间，不可处理");
          return;
        }
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      }
    }
  }
</script>
